package cn.xuqiudong.backend.base.controller;

import cn.xuqiudong.backend.base.request.CheckNotRepeatRequest;
import cn.xuqiudong.common.base.model.BaseGenericEntity;
import cn.xuqiudong.common.base.model.BaseResponse;
import cn.xuqiudong.common.base.model.PageInfo;
import cn.xuqiudong.common.base.service.BaseGenericService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;

/**
 * 描述:
 * 后端controller的基类 继承 注入的是 BaseGenericService 的子类，并且主键默认为Integer
 *
 * @author Vic.xu
 * @since 2025-02-07 16:55
 */
public abstract class
BaseGenericBackendController<S extends BaseGenericService<?, T, java.lang.Integer>,
        T extends BaseGenericEntity<java.lang.Integer>> {

    protected Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired
    protected S service;

    @Resource
    protected HttpServletRequest request;


    @Resource
    protected HttpServletRequest response;


    /**
     * 分页列表
     */
    @PostMapping(value = "/page")
    public BaseResponse<PageInfo<T>> page(@RequestBody T entity) {
        return BaseResponse.success(service.page(entity));
    }

    /**
     * 详情
     */
    @GetMapping(value = "/detail/{id}")
    public BaseResponse<T> detail(@PathVariable Integer id) {
        return BaseResponse.success(service.findById(id));
    }


    /**
     * 新增/修改
     */
    @PostMapping(value = "/save")
    public BaseResponse<Integer> save(@RequestBody T entity) {
        return BaseResponse.success(service.save(entity));
    }


    /**
     * 删除
     */
    @PostMapping(value = "/delete/{id}")
    public BaseResponse<Integer> delete(@PathVariable Integer id) {
        return BaseResponse.success(service.delete(id));
    }

    /**
     * 检测字段是否可用
     */
    @PostMapping(value = "/check")
    public BaseResponse<?> check(@RequestBody CheckNotRepeatRequest repeatRequest) {
        boolean ok = service.checkNotRepeat(repeatRequest.getId(), repeatRequest.getValue(), repeatRequest.getColumn());
        return BaseResponse.success(ok);
    }


}
